﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CRMSystemV2.DBUtility;
namespace CRMSystemV2.SQLServerDAL
{
    public class Pager : CRMSystemV2.IDAL.IPager
    {
        /// <summary>
        /// 由于采用dynamic方式实现反射，不知道传出参数如何取值，故封装成属性取值
        /// </summary>        
        private Dictionary<string, int> dic = new Dictionary<string, int>();
        public DataTable GetPageView(int pageSize, int pageIndex, string sql)
        {
            SqlParameter[] pars = new SqlParameter[] { 
            new SqlParameter("@ip_statement",SqlDbType.Text),
            new SqlParameter("@ip_pageindex",SqlDbType.Int),
            new SqlParameter("@ip_pagesize",SqlDbType.Int),
            new SqlParameter("@op_rowtotal",SqlDbType.Int),
            new SqlParameter("@op_pagetotal",SqlDbType.Int)
            };
            pars[0].Value = sql;
            pars[1].Value = pageIndex;
            pars[2].Value = pageSize;
            pars[3].Direction = ParameterDirection.Output;
            pars[4].Direction = ParameterDirection.Output;
            DataSet ds = SQLHelper.RunProcedure(SQLHelper.ConnectionString, "sp_pageview", "result", pars);
            dic.Clear();
            dic.Add("RowCount", int.Parse(pars[3].Value.ToString()));
            dic.Add("PageCount", int.Parse(pars[4].Value.ToString()));
            return ds.Tables[1];
        }
        /// <summary>
        /// 由于采用dynamic方式实现反射，不知道传出参数如何取值，故封装成属性取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int GetValue(string key)
        {
            if (dic.ContainsKey(key))
                return dic[key];
            else
                return 0;
        }
    }
}
